package com.jianganming.factory.abstracts;


/**
 * 抽象工厂：
 *   抽象发动机工厂：制造发动机
 *   抽象轮胎工厂：制造轮胎
 *   抽象汽车工厂：制造汽车
 * 具体工厂：中国发动机工厂，美国发动机工厂，奔腾轮胎工厂，万达轮胎工厂，宝马汽车工厂，奔驰汽车工厂
 * 实例对象：发动机对象(名称，排量)，轮胎对象(名称，尺寸)，汽车对象(名称，发动机对象，轮胎对象)
 * 宝马汽车工厂对象(发动机工厂对象，轮胎工厂对象)
 * 奔驰汽车工厂对象(发动机工厂对象，轮胎工厂对象)
 *
 *
 */
public class AbstractFactory {
    public static void main(String[] args) {
        EngineFactory engineFactory = new ChinaEngineFactory();
        EngineFactory engineFactory1 = new UsaEngineFactory();
        TyreFactory tyreFactory = new OneTyreFactory();
        TyreFactory tyreFactory1 = new TwoTyreFactory();

        CarFactory carFactory = new BaomaCarFactory(engineFactory,tyreFactory1);
        Car car = carFactory.makeCar();
        System.out.println(car.getName());
        System.out.println(car.getEngine());
        System.out.println(car.getTyre());
        System.out.println("----------------------------------------------");
        CarFactory carFactory1 = new BenchiFactory(engineFactory1,tyreFactory);
        Car car1 = carFactory1.makeCar();
        System.out.println(car1.getName());
        System.out.println(car1.getEngine());
        System.out.println(car1.getTyre());

    }
}

/**
 * 抽象发动机工厂
 */
interface EngineFactory{
    Engine makeEngine();
}

/**
 * 具体发动机工厂：长城发动机
 */
class ChinaEngineFactory implements EngineFactory{

    @Override
    public Engine makeEngine() {
        return new Engine("长城发动机","2.0");
    }
}

/**
 * 具体发动机工厂：美国发动机
 */
class UsaEngineFactory implements EngineFactory{

    @Override
    public Engine makeEngine() {
        return new Engine("美国发动机","1.8");
    }
}

interface TyreFactory{
    Tyre makeTyre();
}

class OneTyreFactory implements TyreFactory{

    @Override
    public Tyre makeTyre() {
        return new Tyre(30,"万达");
    }
}

class TwoTyreFactory implements TyreFactory{

    @Override
    public Tyre makeTyre() {
        return new Tyre(40,"奔腾");
    }
}

interface CarFactory{
    Car makeCar();
}


class BaomaCarFactory implements CarFactory{
    private EngineFactory engineFactory;
    private TyreFactory tyreFactory;

    public BaomaCarFactory(EngineFactory engineFactory, TyreFactory tyreFactory) {
        this.engineFactory = engineFactory;
        this.tyreFactory = tyreFactory;
    }

    @Override
    public Car makeCar() {
        Engine engine = engineFactory.makeEngine();
        Tyre tyre = tyreFactory.makeTyre();
        return new Car("宝马",engine,tyre);
    }
}

class BenchiFactory implements CarFactory{
    private EngineFactory engineFactory;
    private TyreFactory tyreFactory;

    public BenchiFactory(EngineFactory engineFactory, TyreFactory tyreFactory) {
        this.engineFactory = engineFactory;
        this.tyreFactory = tyreFactory;
    }

    @Override
    public Car makeCar() {
        Engine engine = engineFactory.makeEngine();
        Tyre tyre = tyreFactory.makeTyre();
        return new Car("奔驰",engine,tyre);
    }
}

class Car{
    private String name;
    private Engine engine;
    private Tyre tyre;

    public Car(String name,Engine engine, Tyre tyre) {
        this.name = name;
        this.engine = engine;
        this.tyre = tyre;
    }

    public Engine getEngine() {
        return engine;
    }

    public void setEngine(Engine engine) {
        this.engine = engine;
    }

    public Tyre getTyre() {
        return tyre;
    }

    public void setTyre(Tyre tyre) {
        this.tyre = tyre;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


class Tyre{
    private Integer size;
    private String brand;

    public Tyre(Integer size, String brand) {
        this.size = size;
        this.brand = brand;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    @Override
    public String toString() {
        return "Tyre{" +
                "size=" + size +
                ", brand='" + brand + '\'' +
                '}';
    }
}


class Engine{
    private String name;
    private String displacement;

    public Engine(String name, String displacement) {
        this.name = name;
        this.displacement = displacement;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDisplacement() {
        return displacement;
    }

    public void setDisplacement(String displacement) {
        this.displacement = displacement;
    }

    @Override
    public String toString() {
        return "Engine{" +
                "name='" + name + '\'' +
                ", pailiang='" + displacement + '\'' +
                '}';
    }
}
